package com.gxa.autoparts.admin.config;

import com.gxa.autoparts.admin.listener.OrderListener;

import org.springframework.amqp.core.AcknowledgeMode;


import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author R
 * @data 2022-01-07 10:10
 */
@Configuration
public class ListenerConfig {

    @Autowired
    private CachingConnectionFactory connectionFactory;

    @Autowired
    private OrderListener orderListener;//消息接收处理类

    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setConcurrentConsumers(1);
        container.setMaxConcurrentConsumers(1);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL); // RabbitMQ默认是自动确认，这里改为手动确认消息
        //设置一个队列
//        container.setQueueNames("topic.man");
        //如果同时设置多个如下： 前提是队列都是必须已经创建存在的
          container.setQueueNames("sellQueue","dlxQueue");
        //另一种设置队列的方法,如果使用这种情况,那么要设置多个,就使用addQueues
//        container.setQueues(new Queue("sellQueue",true));
//        container.addQueues(new Queue("orderQueue",true));
//        container.addQueues(new Queue("dlxQueue",true));
        container.setMessageListener(orderListener);

        return container;
    }

}
